package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.DeviceCategoryMapper;
import com.yunhai.device.mapper.FmsCodeDictMapper;
import com.yunhai.device.mapper.ProductCategoryMapper;
import com.yunhai.device.service.DeviceCategoryService;
import com.yunhai.model.entity.dms.DmsDeviceCategoryEntity;
import com.yunhai.model.entity.dms.DmsProductCategoryEntity;
import com.yunhai.model.entity.fms.FmsCodeDictEntity;
import com.yunhai.model.vo.dms.DmsDeviceCategoryQuery;
import com.yunhai.model.vo.dms.DmsDeviceCategoryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DeviceCategoryServiceImpl implements DeviceCategoryService {

    @Resource
    private DeviceCategoryMapper deviceCategoryMapper;
    @Resource
    private FmsCodeDictMapper fmsCodeDictMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 获取 DeviceCategory 分页列表
     */
    @Transactional
    public IPage getDeviceCategoryPagination(PaginationVo paginationVo, DmsDeviceCategoryQuery dmsDeviceCategoryQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsDeviceCategoryEntity> lqs = new LambdaQueryWrapper();
        lqs.eq( dmsDeviceCategoryQuery.getCategoryId() != null,
                DmsDeviceCategoryEntity::getCategoryId,
                dmsDeviceCategoryQuery.getCategoryId()
        );
        lqs.likeRight(dmsDeviceCategoryQuery.getName() != null,
                DmsDeviceCategoryEntity::getName,
                dmsDeviceCategoryQuery.getName() );
        lqs.orderByDesc( DmsDeviceCategoryEntity::getCreateTime );
        deviceCategoryMapper.selectPage(page, lqs);

        List<DmsDeviceCategoryEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        Set<Long> categoryIdSet = record.stream().map(DmsDeviceCategoryEntity::getCategoryId).collect(Collectors.toSet());
        List<DmsProductCategoryEntity> categoryItems = productCategoryMapper.selectBatchIds(categoryIdSet);
        if( categoryItems != null ){
            for( DmsDeviceCategoryEntity item : record  ){
                categoryItems.stream().filter(row->row.getId().equals(item.getCategoryId())).findFirst().ifPresent(row->{
                    item.setCategoryName( row.getName() );
                });
            }
        }


        return page;
    }


    /**
     * 获取 DeviceCategory 所有数据
     *
     */
    @Transactional
    public List<DmsDeviceCategoryEntity> getDeviceCategoryAllList( DmsDeviceCategoryQuery dmsDeviceCategoryQuery ) throws Exception {

        LambdaQueryWrapper<DmsDeviceCategoryEntity> lqs = new LambdaQueryWrapper();
        List<DmsDeviceCategoryEntity> list = deviceCategoryMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 DeviceCategory 详情
     */
    @Transactional
    public DmsDeviceCategoryEntity getDeviceCategoryDetail( Long id) throws Exception {

        DmsDeviceCategoryEntity dmsDeviceCategoryEntity = deviceCategoryMapper.selectById(id);
        return dmsDeviceCategoryEntity;
    }


    /**
     * 新增 DeviceCategory
     * */
    @Transactional
    public void addDeviceCategory(DmsDeviceCategoryVo dmsDeviceCategoryVo) throws Exception {

        DmsDeviceCategoryEntity dmsDeviceCategoryEntity = new DmsDeviceCategoryEntity();
        ReflectionCopy.copyProperties( dmsDeviceCategoryEntity, dmsDeviceCategoryVo );
        deviceCategoryMapper.insert(dmsDeviceCategoryEntity);

        FmsCodeDictEntity fmsCodeDictEntity = new FmsCodeDictEntity();
        fmsCodeDictEntity.setId(101L);
        fmsCodeDictEntity.setLastCode(dmsDeviceCategoryVo.getNumber());
        fmsCodeDictMapper.updateById(fmsCodeDictEntity);
        return;
    }


    /**
     * 更新 DeviceCategory
     * */
    @Transactional
    public void updateDeviceCategory( DmsDeviceCategoryVo dmsDeviceCategoryVo) throws Exception {

        if( dmsDeviceCategoryVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsDeviceCategoryEntity dmsDeviceCategoryEntity = new DmsDeviceCategoryEntity();
        ReflectionCopy.copyProperties( dmsDeviceCategoryEntity, dmsDeviceCategoryVo );
        deviceCategoryMapper.updateById(dmsDeviceCategoryEntity);
        return;
    }

    /**
     * 删除 DeviceCategory
     * */
    @Transactional
    public void batchRemoveDeviceCategory( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        deviceCategoryMapper.deleteBatchIds(ids);
    }

    @Override
    public List<DmsDeviceCategoryEntity> getProductListByIds(List<Long> list) throws Exception {

        return deviceCategoryMapper.selectBatchIds(list);
    }

}